/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 01
 * Time: 18:01
 */
public class LinkedList {
    static class LinkNode {
        public int val;
        public LinkNode prev;
        public LinkNode next;
        public LinkNode(int data) {
            this.val = data;
        }
    }
    public LinkNode head;
    public LinkNode last;

    //得到单链表的长度
    public int size() {
        int count = 0;
        LinkNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        LinkNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        LinkNode cur = new LinkNode(data);
        if(head == null) {
            head = cur;
            last = cur;
            return;//如果有else这里就不用加return
        }
        cur.next = head;
        head.prev = cur;
        head = cur;
    }

    //尾插法
    public void addLast(int data) {
        LinkNode cur = new LinkNode(data);
        if(last == null) {
            head = cur;
            last = cur;
            return;
        }
        last.next = cur;
        cur.prev = last;
        last = cur;//或last=last.next
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if((index < 0) || (index > size())) {
            throw new IndexOutOfException(index + "下标不合法");
        }
        if(index == 0) {
            addFirst(data);
            return true;
        }
        if(index == size()) {
            addLast(data);
            return true;
        }
        LinkNode node = new LinkNode(data);
        LinkNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;//index==size()时这样插入不行，所以index==size()时要单独用尾插法插入
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
        return true;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            System.out.println("链表为空");
            return;
        }
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur.prev == null) {//删除头节点
                    if(cur.next != null) {
                        head = cur.next;
                        head.prev = null;
                    }else {//只有一个节点，且这个节点是要删除的节点
                        head = cur.next;
                        last = null;
                    }
                }else {
                    if(cur.next == null) {//删除尾节点
                        cur.prev.next = cur.next;
                        last = cur.prev;
                    }else {
                        cur.prev.next = cur.next;//删除中间节点
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
        System.out.println("没有要删除的数据");
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            System.out.println("链表为空");
            return;
        }
        LinkNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur.prev == null) {//删除头节点
                    if(cur.next != null) {
                        head = cur.next;
                        head.prev = null;
                    }else {//只有一个节点，且这个节点是要删除的节点
                        head = cur.next;
                        last = null;
                    }
                }else {
                    if(cur.next == null) {//删除尾节点
                        cur.prev.next = cur.next;
                        last = cur.prev;
                    }else {
                        cur.prev.next = cur.next;//删除中间节点
                        cur.next.prev = cur.prev;
                    }
                }
                cur = cur.next;
            }else{
                cur = cur.next;
            }
        }
    }

    public void clear() {
        LinkNode cur = head;
        while(cur != null) {//将每个节点的next和prev置为null
            LinkNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;//因为执行完上面的代码之后，链表断开了，但头结点和尾节点仍有head和last在引用
        last = null;
    }
}
